package com.ksyzt.ksite2.client.orgnization;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.ksyzt.gwt.client.event.HasMessageHandlers;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.shared.modle.ORGANIZATIONObj;
import com.ksyzt.ksite2.shared.modle.ORGANIZATIONObjs;

/**
 * 编辑组织树
 *
 * @author 张建设
 *
 */
public class OrgnizationTree extends Tree implements HasMessageHandlers {

	private SelectionHandler<TreeItem> m_selection_handler = new SelectionHandler<TreeItem>() {

		public void onSelection(SelectionEvent<TreeItem> event) {

			TreeItem it = event.getSelectedItem();
			if (it.getUserObject() == null) {
				MessageEvent e = new MessageEvent(MessageEvent.CREATEROOT, null);
				fireEvent(e);
			} else {
				MessageEvent e = new MessageEvent(MessageEvent.ITEMCLICK, it);
				fireEvent(e);
			}
		}
	};

	public OrgnizationTree() {
		super();
		this.addSelectionHandler(m_selection_handler);
	}

	private AsyncCallback<ORGANIZATIONObjs> on_root_data = new AsyncCallback<ORGANIZATIONObjs>() {

		public void onSuccess(ORGANIZATIONObjs result) {

			m_map.clear();
			parseRoot(result);
		}

		public void onFailure(Throwable caught) {

			message(caught.getMessage());
		}
	};

	private void parseRoot(ORGANIZATIONObjs result) {

		if (result.size() > 0) {
			for (int i = 0; i < result.size(); i++) {
				ORGANIZATIONObj o = result.get(i);

				TreeItem item = this.addItem(o.getNAME());
				item.setUserObject(o);
				getChildItem(item, o);
				m_map.put(o.getID(), item);
			}
			this.setSelectedItem(this.getItem(0), true);
		}

		if (m_bCreate == true) {
			if (result.size() == 0) {
				// 没有跟组织信息 需要创建一个
				this.add(new Label("创建" + m_orgtypename));
			} else {
				if (m_onlyone == false) {
					// 允许创建多个跟组织
					this.add(new Label("创建另一个" + m_orgtypename));
				}
			}
		}
	}

	private void getChildItem(TreeItem item, ORGANIZATIONObj o) {
		ServerProxy.SYS_MANAGER.getAllChildrens(o.getID(), on_all_chidren);
	}

	private AsyncCallback<ORGANIZATIONObjs> on_all_chidren = new AsyncCallback<ORGANIZATIONObjs>() {

		public void onSuccess(ORGANIZATIONObjs result) {

			for (int i = 0; i < result.size(); i++) {
				ORGANIZATIONObj o = result.get(i);
				TreeItem p = m_map.get(o.getPID());
				if (p != null) {
					TreeItem it = p.addItem(o.getNAME());
					it.setUserObject(o);
					m_map.put(o.getID(), it);
					p.setState(true);
				}
			}
		}

		public void onFailure(Throwable caught) {
			message(caught.getMessage());

		}
	};

	public TreeItem getTreeItem(int oid) {
		return m_map.get(oid);
	}

	protected void message(String message) {
		MessageEvent e=new MessageEvent(MessageEvent.MESSAGE,message);
		fireEvent(e);
		
	}

	Map<Integer, TreeItem> m_map = new HashMap<Integer, TreeItem>();
	int m_userid = 0;
	int m_orgtype = 0;
	boolean m_onlyone = true;
	String m_orgtypename = "";

	/**
	 * 初始化组织树
	 *
	 * @param userid
	 *            该组织树所属用户ID
	 * @param orgtype
	 *            要编辑的组织类型ID
	 * @param orgtypename
	 *            组织类型名称
	 * @param onlyone
	 *            是否只能创建一个跟组织
	 */
	public void initTree(int userid, int orgtype, String orgtypename,
			boolean onlyone) {
		this.clear();
		m_userid = userid;
		m_orgtype = orgtype;
		m_onlyone = onlyone;
		m_orgtypename = orgtypename;

		ServerProxy.SYS_MANAGER.getOrganizations(userid, orgtype, on_root_data);
	}

	boolean m_bCreate = true;

	public void EnableCreate(boolean b) {
		m_bCreate = b;
	}

	public HandlerRegistration addMessageHandler(MessageHandler handler) {

		return this.addHandler(handler, MessageEvent.TYPE);
	}
}
